﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing.Design;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Windows.Forms;

namespace LegtionMVVM
{
    [DisplayName("LegtionMVVM")]
    [
    ProvideProperty("Binding", typeof(Control)),//开启
    ProvideProperty("Command", typeof(Control))//绑定字段
    ]
    public class LegtionMVVMBinder : System.ComponentModel.Component, IExtenderProvider
    {
        #region binder变量
        const string category = "用LegtionMVVM扩展属性";
        internal Dictionary<Control, string> bindingList = new Dictionary<Control, string>();
        internal Dictionary<Control, string> commandClassList = new Dictionary<Control, string>();        
        #endregion
        #region 基本成员
        private System.ComponentModel.Container components = null;
        public LegtionMVVMBinder(System.ComponentModel.IContainer container)
        {
            InitializeComponent();
            //container.Add(this);
        }
 
        public LegtionMVVMBinder()
        {            
            InitializeComponent();
        }
        /// <summary> 
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #endregion
        #region 组件设计器生成的代码
        /**/
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
        }
        #endregion

        #region IExtenderProvider 成员
        public bool CanExtend(object extendee)
        {
            if (extendee is Control && !(extendee is LegtionMVVMBinder))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion
      
        #region 对应的ProvideProperty 
        #region Binding
        [DefaultValue(""), Category(category),
        DisplayName("Binding"), Description("配置控件的绑定绑定信息，以调用LegtionMVVM"),
        Editor(typeof(BindingEditor), typeof(UITypeEditor))]
        public string GetBinding(Control component)
        {
            try
            {
                return bindingList.ContainsKey(component) ? bindingList[component] : "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return "";
        }
        public void SetBinding(Control component, string value)
        {
            value = value.TrimEnd();
            if (!bindingList.ContainsKey(component))
                bindingList.Add(component, value);
            bindingList[component] = value;
        }
        #endregion
        #region Command
        [DefaultValue(""), Category(category),
   DisplayName("Command"), Description("配置控件的命令绑定信息，以调用LegtionMVVM"),
   Editor(typeof(CommandEditor), typeof(UITypeEditor))]
        public string GetCommand(Control component)
        {
            return commandClassList.ContainsKey(component) ? commandClassList[component] : "";
        }
        public void SetCommand(Control component, string value)
        {
            value = value.TrimEnd();
            if (!commandClassList.ContainsKey(component))
                commandClassList.Add(component, value);
            commandClassList[component] = value;
        }
        #endregion
        #endregion 
    }
}
